package com.levi.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.levi.enums.EnumUtil;
import lombok.Data;
import org.springframework.validation.BindingResult;

import java.util.Objects;

/**
 * @author shentong
 * @since 2022/3/23 8:46 PM
 */

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)  // 不返回值为null的属性
public class ResponseVo<T> {

    private Integer status;

    private String msg;

    private T data;

    public ResponseVo(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public ResponseVo(Integer status, T data) {
        this.status = status;
        this.data = data;
    }


    public static <T> ResponseVo<T> success(String msg) {
        return new ResponseVo<T>(EnumUtil.SUCCESS.getCode(), msg);
    }

    public static <T> ResponseVo<T> success( ) {
        return new ResponseVo<T>(EnumUtil.SUCCESS.getCode(),EnumUtil.SUCCESS.getMsg());
    }

    public static <T> ResponseVo<T> success(T data) {
        return new ResponseVo<T>(EnumUtil.SUCCESS.getCode(), data);
    }

    public static <T> ResponseVo<T> error(EnumUtil enumUtil) {
        return new ResponseVo<T>(enumUtil.getCode(), enumUtil.getMsg());
    }

    public static <T> ResponseVo<T> error(EnumUtil enumUtil, String msg) {
        return new ResponseVo<T>(enumUtil.getCode(), msg);
    }

    public static <T> ResponseVo<T> error(EnumUtil enumUtil, BindingResult bindingResult) {
        return new ResponseVo<T>(enumUtil.getCode(), "注册的参数有误, "+Objects.requireNonNull(bindingResult.getFieldError()).getField()+" "+bindingResult.getFieldError().getDefaultMessage());
    }

}
